package crowMessenger;

import java.awt.Color;
import java.util.Collection;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

public class Account implements Runnable{
	private String username;
	private String password;
	private boolean isActivated;
	private int status;//0=available, 1=busy, 2=away, 3=invisible, 4=disconnected
	private ConnectionConfiguration connectionConf = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
	private XMPPConnection connection;
	private Roster roster;
	protected MainPanel con;
	
	/*Constructor*/
	public Account(String user, String pass, MainPanel con){
		this.con = con;
		connection = new XMPPConnection(connectionConf);
		setUser(user);
		setPass(pass);
		isActivated = false;
		status = 4; //by default it is disconnected
	}

	/*Mutator*/
	protected void setUser(String user){this.username = user;}
	protected void setPass(String pass){this.password = pass;}
	protected void setStatus(int status){this.status = status;}
	protected void activate(){
		run();//login in a new thread
		isActivated = true;
		this.status = 0;
	}
	protected void deactivate(){isActivated = false; this.status = 4;}

	/*Accessor*/
	protected String getUser(){return(username);}
	protected String getPass(){return(password);}
	protected int getStatus(){return(status);}
	protected boolean isActivated(){return(isActivated);}

	/* A thread which handles the login to google */
	@Override 
	public void run() {
		try {
			connection.connect();
		} catch (XMPPException e) {
			e.printStackTrace();
		}
		try {
			connection.login(getUser(), getPass());
			System.out.println("Logged in as "+getUser());
			Presence presence = new Presence(Presence.Type.available);
			presence.setStatus("On with Crow-Testing...");
			connection.sendPacket(presence);
		} catch (XMPPException e) {
			e.printStackTrace();
		}
		
		/* Getting the buddy list and send it to the painter panel(jlist in the mainpanel)*/
		roster = connection.getRoster();
		con.myList.addRosterConnection(roster, connection,this);
		
	}
}
